#coding = utf-8
import urllib.request
import re
import sys
import os

url = sys.argv[-2]
path = sys.argv[-1]
def getHtml():
    name=getName(url)
    urllib.request.urlretrieve(url, path+'/%s' % name)
    page = urllib.request.urlopen(url)
    html = page.read()
    return html
 
def getImg(html):
    imgreg = '<img.*?src="(.*?)"'
    cssreg = '<link.*?href="(.*?.css)"'
    jsreg = '<script.*?src="(.*?)"'
    imgre = re.compile(imgreg)
    jsre = re.compile(jsreg)
    cssre = re.compile(cssreg)
    html = html.decode(encoding='UTF-8',errors='strict')
    imglist = re.findall(imgre, html)
    jslist = re.findall(jsre, html)
    csslist = re.findall(cssre, html)
    abUrl = url[:url.rfind("/")+1]
    imgPath=''
    for imgurl in imglist:
        name = getName(imgurl)
        if imgurl.find("statics")>=0:
            imgPath=path+'/statics/images/'
            urllib.request.urlretrieve(abUrl+imgurl, path+'/statics/images/%s' % name)
        else:
            imgPath=path+'/images/'
            urllib.request.urlretrieve(abUrl+imgurl, path+'/images/%s' % name) 
    for jsurl in jslist:
        name = getName(jsurl)
        if jsurl.find("statics") >= 0:
            urllib.request.urlretrieve(abUrl+jsurl, path+'/statics/js/%s' % name)
        else:
            urllib.request.urlretrieve(abUrl+jsurl, path+'/js/%s' % name)
    for cssurl in csslist:
        name = getName(cssurl)
        cssPath=''
        if cssurl.find("statics") >= 0:
            cssPath=path+'/statics/css/%s' % name
        else:
            cssPath=path+'/css/%s' % name
        urllib.request.urlretrieve(abUrl+cssurl, cssPath)
        if name.find(".min") == -1 or name.find(".min") == -1:
            ireg='background:.*?url\("(.*?)"\)';
            ire=re.compile(ireg)
            print(cssPath)
            iList=[]
            with open(cssPath, 'r+', encoding='UTF-8') as f:
                css=f.read()
                iList = re.findall(ire, css)
            print(iList)
            cssTUrl=abUrl+cssurl
            cssAbUrl=cssTUrl[:cssTUrl.rfind("/")]
            cssAbUrl=cssAbUrl[:cssAbUrl.rfind("/")+1]
            print(cssAbUrl)
            for csurl in iList:
                iname = getName(csurl)
                print(cssAbUrl+csurl.split('../')[-1])
                urllib.request.urlretrieve(cssAbUrl+csurl.split('../')[-1], imgPath+'%s' % iname)
    return "success"
def getName(url):
    return url.split("/")[-1]
def mkdir(path):
    # 去除首位空格
    path=path.strip()
    # 去除尾部 \ 符号
    path=path.rstrip("\\")
 
    # 判断路径是否存在
    # 存在     True
    # 不存在   False
    isExists=os.path.exists(path)
 
    # 判断结果
    if not isExists:
        # 如果不存在则创建目录
        # 创建目录操作函数
        os.makedirs(path) 
 
        print(path+' 创建成功')
        return True
    else:
        # 如果目录存在则不创建，并提示目录已存在
        print(path+' 目录已存在')
        return False 
mkdir(path)
mkdir(path+'/images')
mkdir(path+'/css')
mkdir(path+'/js')
mkdir(path+'/statics/images')
mkdir(path+'/statics/css')
mkdir(path+'/statics/js')
html = getHtml()
 
print (getImg(html))

